package com.ruoyi.project.im.webSocket;

import com.alibaba.fastjson.JSON;
import com.ruoyi.project.im.friends.domain.Friends;
import com.ruoyi.project.im.friends.service.IFriendsService;
import com.ruoyi.project.im.users.domain.Users;
import com.ruoyi.project.im.users.service.IUsersService;
import com.ruoyi.project.im.utils.SpringContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@ServerEndpoint("/ws/{userId}")
@Component
public class WebSocketServer extends WebSocketServerSupport{

    @Autowired
    private IFriendsService friendsService;

    @Autowired
    private IUsersService usersService;

    public static WebSocketServer socketServer;


    @PostConstruct
    public void init(){
        socketServer = this;
        socketServer.friendsService = this.friendsService;
        socketServer.usersService = this.usersService;
    }

    @Override
    public void getAllOnLineFriends(String userId) {
        Users users = socketServer.usersService.selectUsersByUserId(userId);
        if (users != null){
            List<Friends> friends = socketServer.friendsService.selectAllFriendsByOwnerUserId(userId);
            List<String> ids = new ArrayList<>();

            for (Friends friend: friends) {
                if (webSocketMap.containsKey(friend.getFriendUserId())){
                    ids.add(friend.getFriendUserId());
                }
            }
            RequestMethod method = new RequestMethod();
            method.type = 1000;
            method.data = JSON.toJSONString(ids);
            method.event = "OnAllFriendOnLine";
            sendMessage(JSON.toJSONString(method));
        }
    }

    @Override
    public void userOnLine(String userId) {
        Users users = socketServer.usersService.selectUsersByUserId(userId);
        if (users != null){
            List<Friends> friends = socketServer.friendsService.selectAllFriendsByOwnerUserId(userId);
            RequestMethod method = new RequestMethod();
            method.type = 1001;
            method.data = userId;
            method.event = "OnFriendOnLine";
            for (Friends friend: friends) {
                if (webSocketMap.containsKey(friend.getFriendUserId())){
                    sendInfo(JSON.toJSONString(method),friend.getFriendUserId());
                }
            }
        }
    }

    @Override
    public void userOnOff(String userId) {
        Users users = socketServer.usersService.selectUsersByUserId(userId);
        if (users != null){
            List<Friends> friends = socketServer.friendsService.selectAllFriendsByOwnerUserId(userId);
            RequestMethod method = new RequestMethod();
            method.type = 1002;
            method.data = userId;
            method.event = "OnFriendOffLine";
            for (Friends friend: friends) {
                if (webSocketMap.containsKey(friend.getFriendUserId())){
                    sendInfo(JSON.toJSONString(method),friend.getFriendUserId());
                }
            }
        }
    }


}
